package virtPresenter.model
{	
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	
	import mx.controls.Alert;
	
	import virtPresenter.event.SpeedareaScrollEvent;
	
	/**
	 * This class represents both Speedareas, including its graphical objects and its logic.
	 * 
	 * @author: Sebastian Pospiech
	 * */
	public class Speedareas
	{
		
		private static var instance:Speedareas;		
		
		private var speedarea_left_anim:SpeedareaButton;
		private var speedarea_left_slides:SpeedareaButton;
		private var speedarea_right_anim:SpeedareaButton;
		private var speedarea_right_slides:SpeedareaButton;
		private var bottomLeft:Loader = new Loader();
		private var bottomRight:Loader = new Loader();
		
		private var locator:virtPresenterLocator=virtPresenterLocator.getInstance();	
		
		public function Speedareas(enforcer:Class)
		{	
			if (enforcer!=SingletonEnforcer)
				throw new Error("Speedareas is a singleton class, use getInstance instead!");
		 	initializeLeft();
			initializeRight();
			initializeBottom();
		}
		
		public static function getInstance():Speedareas{
			if (instance==null){
				instance = new Speedareas(SingletonEnforcer);
			}
			return instance;
		}
		
		private function initializeLeft():void{
			speedarea_left_anim = new SpeedareaButton("speedarea_left_anim",
				(AS_Constants.SCRUBBER_HEIGHT+AS_Constants.DISTANCE_PREVIEW_SIDES_TO_SCRUBBER),
				 SpeedareaButton.LEFT_ANIM);
			
			speedarea_left_slides = new SpeedareaButton("speedarea_left_slides", 
				(AS_Constants.SCRUBBER_HEIGHT+AS_Constants.DISTANCE_PREVIEW_SIDES_TO_SCRUBBER),
				 SpeedareaButton.LEFT_SLIDES);
				
		}
		
		private function initializeRight():void{
			speedarea_right_anim = new SpeedareaButton("speedarea_right_anim",
				(AS_Constants.SCRUBBER_HEIGHT+AS_Constants.DISTANCE_PREVIEW_SIDES_TO_SCRUBBER),
				 SpeedareaButton.RIGHT_ANIM);
			
			speedarea_right_slides = new SpeedareaButton("speedarea_right_slides", 
				(AS_Constants.SCRUBBER_HEIGHT+AS_Constants.DISTANCE_PREVIEW_SIDES_TO_SCRUBBER),
			     SpeedareaButton.RIGHT_SLIDES);
		}
		
		private function initializeBottom():void{
			bottomLeft.contentLoaderInfo.addEventListener(Event.COMPLETE, bottomLoadLeftComplete);
			bottomLeft.load(new URLRequest("gfx/speedarea_bottom.gif"));
			
			bottomRight.contentLoaderInfo.addEventListener(Event.COMPLETE, bottomLoadRightComplete);
			bottomRight.load(new URLRequest("gfx/speedarea_bottom.gif"));
		}
		
		private function bottomLoadLeftComplete(e:Event):void{
			bottomLeft.height =AS_Constants.SPEEDAREA_BOTTOM_HEIGHT;
			bottomLeft.width = AS_Constants.SPEEDAREA_BOTTOM_WIDTH;
			bottomLeft.visible = false;
			bottomLeft.y = AS_Constants.SPEEDAREA_BOTTOM_Y;
			virtPresenterLocator.getInstance().scrubber.addChild(bottomLeft);
		}
		
		private function bottomLoadRightComplete(e:Event):void{
			bottomRight.height = AS_Constants.SPEEDAREA_BOTTOM_HEIGHT;
			bottomRight.width = AS_Constants.SPEEDAREA_BOTTOM_WIDTH;
			bottomRight.visible = false;
			bottomRight.y = AS_Constants.SPEEDAREA_BOTTOM_Y;
			virtPresenterLocator.getInstance().scrubber.addChild(bottomRight);
		}
		
		public function setParent(sprite:Sprite):void{
			this.speedarea_left_anim.addParent(sprite);
			this.speedarea_left_slides.addParent(sprite);
			this.speedarea_right_anim.addParent(sprite);
			this.speedarea_right_slides.addParent(sprite);
		}
		
		public function redraw(leftIsVisible:Boolean, rightIsVisible:Boolean, sprite:Sprite):void{
			speedarea_left_anim.isVisible(false);
			speedarea_left_slides.isVisible(false);
			speedarea_right_anim.isVisible(false);
			speedarea_right_slides.isVisible(false);
			bottomLeft.visible = false;
			bottomRight.visible = false;
			sprite.graphics.lineStyle(2, 0x000000);
			sprite.graphics.endFill();
			
			if (leftIsVisible){
				redrawLeft(sprite);
				sprite.graphics.drawRect(-200-(130*2/3) - locator.scrubber.calculatePreviewPosition(),AS_Constants.SCRUBBER_HEIGHT+AS_Constants.DISTANCE_PREVIEW_SIDES_TO_SCRUBBER,130*2/3,AS_Constants.SCRUBBER_HEIGHT + 90);
			} 
			if (rightIsVisible){
				redrawRight(sprite);
				sprite.graphics.drawRect(200 - locator.scrubber.calculatePreviewPosition(),AS_Constants.SCRUBBER_HEIGHT+AS_Constants.DISTANCE_PREVIEW_SIDES_TO_SCRUBBER,130*2/3,AS_Constants.SCRUBBER_HEIGHT + 90);
			}	
		}
		
		private function redrawLeft(sprite:Sprite):void{
			speedarea_left_slides.isVisible(true);
			speedarea_left_slides.setX(sprite.x-200-AS_Constants.WIDTH_PREVIEW_SIDES*2/3 - locator.scrubber.calculatePreviewPosition());
			speedarea_left_anim.isVisible(true);
			speedarea_left_anim.setX(sprite.x-200-AS_Constants.WIDTH_PREVIEW_SIDES*1/3 - locator.scrubber.calculatePreviewPosition());
			bottomLeft.visible = true;
			bottomLeft.x = sprite.x-200+1-AS_Constants.WIDTH_PREVIEW_SIDES*2/3 - locator.scrubber.calculatePreviewPosition();
			
		}
		
		private function redrawRight(sprite:Sprite):void{
			speedarea_right_anim.isVisible(true);
			speedarea_right_anim.setX(sprite.x + 200 - locator.scrubber.calculatePreviewPosition());
	
			speedarea_right_slides.isVisible(true);
			speedarea_right_slides.setX(sprite.x + 200 + AS_Constants.WIDTH_PREVIEW_SIDES*1/3 - locator.scrubber.calculatePreviewPosition());
			
			bottomRight.visible = true;
			bottomRight.x = sprite.x + 200 +1 - locator.scrubber.calculatePreviewPosition();
		}
		
		public function activeSpeedareaPresent():Boolean{
			if ((this.speedarea_left_anim.active())
				|| (this.speedarea_left_slides.active())
				|| (this.speedarea_right_anim.active())
				|| (this.speedarea_right_slides.active())){
				return true
			}
			return false;
		}
		
		public function getSpeedareaLeftX():int{
			return this.speedarea_left_slides.getX();
		}
		
		public function getSpeedareaRightX():int{
			return this.speedarea_right_slides.getX() + this.speedarea_right_slides.getWidth();
		}
		
		public function speedScrub():void{
			if (this.speedarea_left_anim.active()==true){
				CairngormEventDispatcher.getInstance().dispatchEvent(new SpeedareaScrollEvent(true, SpeedareaScrollEvent.ANIMATIONS));
			} else if (this.speedarea_left_slides.active()==true){
				CairngormEventDispatcher.getInstance().dispatchEvent(new SpeedareaScrollEvent(true, SpeedareaScrollEvent.SLIDES));
			} else if (this.speedarea_right_anim.active()==true){
				CairngormEventDispatcher.getInstance().dispatchEvent(new SpeedareaScrollEvent(false, SpeedareaScrollEvent.ANIMATIONS));
			} else if (this.speedarea_right_slides.active()==true){
				CairngormEventDispatcher.getInstance().dispatchEvent(new SpeedareaScrollEvent(false, SpeedareaScrollEvent.SLIDES));
			} else {
				trace("No Speedarea is active, but speedscrub was invoked!");
			}
		}
		
		public function setActivationOnCoordinates(mouseX:int, mouseY:int):void{
			speedarea_left_anim.setActivation(false);
			speedarea_left_slides.setActivation(false);
			speedarea_right_anim.setActivation(false);
			speedarea_right_slides.setActivation(false);
			
			if (mouseY<speedarea_left_anim.getY()
				|| mouseY>speedarea_left_anim.getY()
				   +speedarea_left_anim.getHeight()){
				return;
			}
			
			if (mouseX<speedarea_left_slides.getX()) 
				return;
			if (mouseX<speedarea_left_anim.getX()){
				speedarea_left_slides.setActivation(true);
				return;
			}
				
			if (mouseX<speedarea_left_anim.getX() 
						+ speedarea_left_anim.getWidth()){
				speedarea_left_anim.setActivation(true);
				return;
			}
				
			if (mouseX<speedarea_right_anim.getX())
				return;
			if (mouseX<speedarea_right_slides.getX()){
				speedarea_right_anim.setActivation(true);
				return;
			}
				
			if (mouseX<speedarea_right_slides.getX()
						+ speedarea_right_slides.getWidth()){
				speedarea_right_slides.setActivation(true);
				return;
			}
						
				
		}
	}
}
class SingletonEnforcer{}